@extends('base')

@section('content')
<form class="layui-form" name="add_form" lay-filter="add_form">
    <div class="mainbox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">父级菜单：</label>
                <div class="layui-input-block">
                    <select name="pid" lay-verify="required|number">
                        <option value="0">顶级菜单</option>
                        @foreach($sub as $v)
                        <option value="{{ $v['id'] }}" @if($pid == $v['id']) selected @endif> ├ {{ $v['menu_name'] }}</option>
                            @foreach($v['child'] as $vv)
                            <option value="{{ $vv['id'] }}" @if($pid == $vv['id']) selected @endif> ├  ├ {{ $vv['menu_name'] }}</option>
                            @endforeach
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单类型：</label>
                <div class="layui-input-block">
                    <input type="radio" name="menu_type" value="1" title="功能" checked>
                    <input type="radio" name="menu_type" value="2" title="菜单">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="menu_name" lay-verify="required" placeholder="请输入菜单名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">链接地址：</label>
                <div class="layui-input-block">
                    <input type="text" name="menu_url" value="#" placeholder="请输入链接地址" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序值：</label>
                <div class="layui-input-block">
                    <input type="number" name="menu_sort" value="0" lay-verify="required|number" placeholder="请输入排序值" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">ICON图标：</label>
                <div class="layui-input-inline">
                    <input type="text" name="menu_icon" id="icon" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单状态：</label>
                <div class="layui-input-block">
                    <input type="radio" name="menu_status" value="1" title="启用" checked>
                    <input type="radio" name="menu_status" value="2" title="停用">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="add-save"><i class="layui-icon layui-icon-ok"></i>提交</button>
            <button type="reset" class="pear-btn pear-btn-sm"><i class="layui-icon layui-icon-refresh"></i>重置</button>
        </div>
    </div>
</form>
@endsection

@section('js')
<script>
    layui.use(['form', 'jquery'], function() {
        let form = layui.form;
        let $ = layui.jquery;

        form.on('submit(add-save)', function(data) {
            $.ajax({
                url: '/menu/add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(result) {
                    if (result.status) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            // parent.layui.table.reload("list-table");
                            parent.render()
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                },
                error: function (xhr) {
                    if(xhr.status !== 200) {
                        layer.msg("网络错误！", {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
@endsection
